package org.openwebtop.sunshine.bo;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.openwebtop.sunshine.dao.UserDAO;
import org.openwebtop.sunshine.model.User;

public class UserBO {
	private UserDAO userDAO;

	public User getUser(String userid) throws Exception {
		return userDAO.selectUserByUserid(userid);
	}

	public User getUser(String userid, String password) throws Exception {
		if (StringUtils.isNotBlank(userid) && StringUtils.isNotBlank(password)) {
			final String hashedPassword = DigestUtils.md5Hex(password);
			final User user = userDAO.selectUserByUserid(userid);

			if (user != null) {
				if (StringUtils.equals(user.getPassword(), hashedPassword)) {
					return user;
				}
			}
		}

		return null;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

}
